﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace HtmlToPic
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。 
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            InitLog4Net();

            var log = LogManager.GetLogger(typeof(Program));

            try
            {
                //一次只运行一个
                if (RunningInstance() == null)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new HtmlToPic(args));
                }
            }
            catch (Exception ex)
            {
                log.Error("全局异常", ex);
            }
        }

        public static System.Diagnostics.Process RunningInstance()
        {
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processes) //查找相同名称的进程 
            {
                if (process.Id != current.Id) //忽略当前进程 
                { //确认相同进程的程序运行位置是否一样. 
                    if (process.ProcessName == current.ProcessName)
                    {
                        return process;
                    }
                }
            }
            return null;
        }

        private static void InitLog4Net()
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);
        }
    }
}
